const itemList = [
    '评论包含欺诈信息',
    '评论包含色情信息',
    '评论包含暴力恐怖信息',
    '评论包含政治敏感信息',
    '评论包含暴力恐怖信息',
    '评论包含政治敏感信息'
];
// 买家评论
const App = getApp();
import { handPic, empty, getUrl } from '../../utils/util.js';
import Api, { ADD_CART, GET_GOODS_DETAIL, GET_GOODS_INFO_BY_SPEC, GOODS_COLLECT, SET_GOODS_HISTORY, GET_GOODS_COMMENT } from '../../utils/api.js';
Page({
    data: {
        info: {},
        picker: {},                     // Picker 数据
        picker_show: false,             // Picker 的显示与隐藏
        chose_spec: [],                 // 选择规格
        chose_spec_value: false,        // 规格值
        buy_num: 1,                     // 购买数量
        page_height: '',                // 页面高度
        toView: '',
        mask: false,
        imgs: [],
        ctype: false,                   // 加入购物车类型,
        key: "",
        confirm: false,                 // true可以加入购物车了
        hasSpec: true,                  // 是否有规格

        itemList: itemList, // 举报列表项内容    ==> 需要添加的数据
        report: '',         // 评论举报内容   
    },
    // scroll 
    scroll: function () {

    },
    // 页面跳转
    navigateTo: function (e) {
        const {tip,idx} = e.currentTarget.dataset;
        let url ;
        // 购物车属于tabBar页面 switchTab
        if (tip == "cart") {
        
            url = getUrl("cart", { ctype: 'normal' });
            wx.reLaunch({
                url: url,
            })
        }else if (tip == 'comment'){
            url = `/pages/all_evaluation/all_evaluation?gc_id=${idx}`;
            App.navigateTo(url);
        }
    },
    //report
    report: function () {
        const that = this;

        wx.showActionSheet({
            itemList: itemList,       // 举报列表项内容
            success: function (res) {
                const idx = res.tapIndex;
                wx.showModal({
                    title: '提示',
                    content: itemList[idx],
                    success: function (res) {
                        if (res.confirm) {
                            /** 确定举报 设置举办内容 */
                            that.setData({
                                report: itemList[idx]
                            })
                        }
                    }
                });
            }
        })
    },
    // ================= Picker 方法区  =================
    // Picker Show and Close
    picker: function (e) {
        const { picker_show, picker } = this.data;
        const _this = this;
        this.setData({
            ctype: e.target.dataset.type
        })
        if (picker_show) {
            this.picker_close();
        } else {
            picker.bottom = true;
            picker.opacity = true;
            this.setData({ picker_show: true });
            setTimeout(() => {
                _this.setData({ picker: picker });
            }, 100)
            // this.picker_show();
        }
    },
    
    picker_show: function () {
        const { picker } = this.data;
        picker.bottom = true;
        picker.opacity = true;
        this.setData({ picker_show: true });
        setTimeout(() => {
            _this.setData({ picker: picker });
        }, 100)
    },

    // Picker Close 
    picker_close: function () {

        const { picker } = this.data;

        // picker 的过渡状态
        picker.bottom = false;
        picker.opacity = false;

        this.setData({
            picker: picker,
        });
        setTimeout(() => {
            this.setData({ picker_show: false });
        }, 300)

    },

    // 产品属性选择
    chose_spec: function (e) {
        const { info } = this.data;
        let chose = e.target.dataset.idx,
            choseids = chose.split(","),
            chose_spec = [],
            chose_spec_value = [];

        info.spec_list.map((val, key) => {
            if (val.spec_id == choseids[0]) {
                val.item.map((v, k) => {
                    if (v.id == choseids[1]) {
                        v.selected = !v.selected;
                        if (v.selected) {
                            chose_spec.push(v.id);
                            chose_spec_value.push(v.name)
                        }
                    } else {
                        v.selected = false;
                    }
                })
            } else {
                val.item.map((v, l) => {
                    if (v.selected) {
                        chose_spec.push(v.id);
                        chose_spec_value.push(v.name)
                    }
                })
            }
        });

        this.setData({
            info: info,
            chose_spec: chose_spec,
            chose_spec_value: chose_spec_value.length ? chose_spec_value : false
        });

        if (info.spec_list.length == chose_spec.length) {
            let spec = chose_spec.join('_');

            Api.fetch(GET_GOODS_INFO_BY_SPEC, { gc_id: info.gc_id, spec: spec }, resp => {
                info.spec_price = resp.price;
                info.goods_id = resp.goods_id;
                info.store = resp.store;
                this.setData({
                    info: info
                },_=>{
                    this.setData({ confirm:true});
                });
            });
        }else{
            this.setData({ confirm: false });
        }

    },
    //加入购物车
    addCart: function () {
        const { key, info, chose_spec, ctype, buy_num, confirm, hasSpec } = this.data;
        let goods_id;
        console.log(info.goods_id);
     
        if (info.spec_list) {
            if (info.spec_list.length != chose_spec.length) {
                wx.showToast({
                    title: '请选择规格',
                    image: '../../img/public/error.png'
                })
                return;
            };
        }
        //快速购买
        if (ctype == 'fastbuy') {
            this.fastbuy();
            return;
        };
        /**  数据请求 */
        if (!hasSpec||confirm){
            Api.fetch(ADD_CART, { key: key, goods_id: info.goods_id, goods_num: buy_num, type: 'normal' }, resp => {
                this.picker_close();

                wx.showToast({
                    title: '成功加入购物车',
                })
            })
        };

        //加入购物车




    },
    //快速购买
    fastbuy: function () {
        const {key,info,buy_num} = this.data;
        Api.fetch(ADD_CART, { key: key, goods_id: info.goods_id, goods_num: buy_num, type: 'fast' }, resp => {
            this.picker_close();
            let url = getUrl("pay", { ctype: 'fast' });
            wx.navigateTo({
                url: url
            })
        })
    },
    // modify number
    modify_number: function (e) {
        const tip = e.target.dataset.tip;
        let { buy_num, info } = this.data;

        if (tip == "add") {
            buy_num += 1;
            buy_num = Math.min(buy_num, info.store);
        } else if (tip == "minus") {
            buy_num -= 1;
            buy_num = Math.max(1, buy_num);
        }

        this.setData({
            buy_num: buy_num
        })
    },


    // ================= Picker 方法区  =================

    msk: function () {
        this.data.mask ?
            this.setData({
                mask: false,
            }) : this.setData({
                mask: true,
            })
    },
    //  ======= 图片预览 ========
    preview: function (e) {
        const { comments } = this.data;
        const { idx, index } = e.target.dataset;
        // index 外层 idx内层
        wx.previewImage({
            current: comments[index].imgs[idx], // 当前显示图片的http链接
            urls: comments[index].imgs // 需要预览的图片http链接列表
        })
        
    },
    imgview:function(e){
        let { idx } = e.currentTarget.dataset,
            {imgs} = this.data;

        wx.previewImage({
            current: imgs[idx], // 当前显示图片的http链接
            urls: imgs // 需要预览的图片http链接列表
        })
    },
    /** 收藏 */
    collect: function (e) {
        const {key,info} = this.data;
        Api.fetch(GOODS_COLLECT, { key: key, goods_commonid: info.gc_id }, resp => {
            info.hasCollect = !info.hasCollect;
            console.log(info.hasCollect);
            this.setData({
                info: info
            })
        })
    },
   
    onLoad: function (options) {

        let _this = this,
            key;
        let { gc_id } = options;

        
        //判断是否登录
        if (Api.checkLogin(function () {
            _this.onLoad(options);
        })) {
            key = Api.getKey();
            this.setData({
                key: key
            })
        }
        // Set page_height
        wx.getSystemInfo({
            success: function (res) {
                _this.setData({
                    page_height: res.windowHeight
                })
            }
        });
        // Set Title 
        options.title ?
            wx.setNavigationBarTitle({ title: options.title })
            : wx.setNavigationBarTitle({ title: "商品详情" });
        // Get picker data 
        Api.fetch(GET_GOODS_DETAIL, { key: key, gc_id: gc_id }, resp => {
            let imgs = [];

            if (resp.imgs) {
                resp.imgs.map((v, k) => {
                    imgs.push(handPic(v, 4))
                })
            };

            resp.default_img = handPic(resp.default_img, 4);
            let status = true;
            if (resp.spec_list === null) { status = false}

            this.setData({
                info: resp,
                imgs: imgs,
                hasSpec: status
            });
        })

        //获取商品评论
        Api.fetch(GET_GOODS_COMMENT,{goods_commonid:gc_id},resp => {
            let {list,count} = resp;
            list.map((val,key) => {
                if(val.imgs){
                    val.imgs.map((v,k)=>{
                        list[key].imgs[k]= handPic(v,4);
                    })
                }
            })

            this.setData({
                comments:list,
                comment_count:count
            })
        })

        Api.fetch(SET_GOODS_HISTORY,{key:key,goods_commonid:gc_id}, resp=>{

        })

    },

    onReady: function () {

    },
    //  锚点
    anchor: function (e) {
        let tip = e.target.dataset.tip;
        if (tip == "details") {
            this.setData({ toView: "details" })
        } else if (tip == "param") {
            this.setData({ toView: "param" })
        } else if (tip == "recommend") {
            this.setData({ toView: "recommend" })
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        const {info} = this.data;
        return {
            title: '众利商城Lite',
            desc: info.name,
            path: '/page/goods_details/goods_details/?gc_id=' + info.gc_id
        }
    }
})